package demo4;

//使用usedSize 实现循环队列
public class MyQueue {
    public int[] elem;
    public int usedSize;
    public int rear;   //尾
    public int front;  //头


    //无参构造
    public MyQueue() {
        this.elem = new int[5];
    }

    //入队
    public void offer(int val) {
        if (isFull()) {
            throw new IsFullException("对列满了!!");
        }
        this.elem[rear] = val;
        this.rear = (this.rear + 1) % this.elem.length;
        usedSize++;
    }

    //出队
    public int poll(){
        if (isEmpty()) {
            throw new IsEmptyException("对列为空");
        }
        int val = this.elem[front];
        this.front = (this.front + 1) % this.elem.length;
        usedSize--;
        return val;
    }

    //获得队顶元素
    public int peek() {
        if (isEmpty()) {
            throw new IsEmptyException("对列为空");
        }
        return this.elem[front];
    }

    //获得有效数字大小
    public int getUsedSize() {
        return this.usedSize;
    }

    //判断队列是不是为空
    public boolean isEmpty() {
        return (this.rear == this.front) && (usedSize == 0);
    }

    //判断队列是不是满
    public boolean isFull() {
        return usedSize == elem.length;
    }

}
